' This form has two modes, ADD and SELECT. When ADD is chosen, a pointer to a new resourceconsumingruletype object is provided and
' only the properties panel is displayed. The properties are all unlocked so the player can edit them. When used in the
' SELECT mode, it exposes all of the ruletypes and places them in the listbox. When the user selects an item from the 
' listbox, it shows the types properties in the panel, although they remain locked. In the SELECT mode, a pointer to a rule
' is supplied and edited on close to match the selected resourceconsumingruletype.

Imports cmplib01

Public Class frmRulePicker
    Inherits System.Windows.Forms.Form

    ' location constants
    Const FORMSELECT_WIDTH As Integer = 440
    Const FORMADD_WIDTH As Integer = 288
    Const PANELADD_LEFT As Integer = 8
    Const PANELSELECT_LEFT As Integer = 160

    Const c_Module As String = "frmRulePicker"

    Private mSelectedRuleType As cmplib01.ResourceConsumingRuleType     ' this is for selection
    Private ptrRule As cmplib01.Rule
    Private ptrNewRuleType As cmplib01.ResourceConsumingRuleType
    Private ptrCGD As cmplib01.CampaignerGameData
    Private selectMode As Boolean = True

    Private mCancelPressed As Boolean = False

    Public Sub New(ByRef cgd As cmplib01.CampaignerGameData, ByRef rultype As cmplib01.ResourceConsumingRuleType, ByVal MapUnitName As String, Optional ByVal forEdit As Boolean = False)
        ' ADDMODE: Edits the provided resourceconsumingruletype (to create a new resourceconsumingruletype)
        Me.New()
        pnlProperties.Left = PANELADD_LEFT
        lstRuleTypes.Visible = False

        Me.Width = FORMADD_WIDTH

        PopulateCoreRuleBox()

        lblMiles.Text = MapUnitName

        ptrNewRuleType = rultype
        ' unlock the attribute boxes
        If forEdit Then Me.txtRuleTypeName.Enabled = False
        NumRange.ReadOnly = False
        txtRuleTypeName.ReadOnly = False
        cboCoreRule.AllowDrop = True
        cboAppliesDuring.AllowDrop = True
        txtRuleText.ReadOnly = False

        ptrCGD = cgd

        selectMode = False

        PopulateAttributes(ptrNewRuleType)

    End Sub

    Public Sub New(ByRef cgd As cmplib01.CampaignerGameData, ByRef rul As cmplib01.Rule, ByVal MapUnitName As String, Optional ByVal forEdit As Boolean = False)
        ' SELECTMODE: Modifies the provided rule to match the selected resourceconsumingruletype's properties on closing.
        Me.New()   ' first, set up the form
        ptrRule = rul
        ptrCGD = cgd

        ' populate the list with the available rules
        Dim de As DictionaryEntry
        Dim key As String

        For Each de In ptrCGD.RuleTypes_g.HT
            key = de.Key
            lstRuleTypes.Items.Add(ptrCGD.RuleTypes_g.Item(key))
        Next

        ' position everything
        pnlProperties.Left = PANELSELECT_LEFT
        lstRuleTypes.Visible = True
        pnlProperties.Visible = True
        Me.Width = FORMSELECT_WIDTH

        If forEdit Then txtRuleTypeName.Enabled = False

        ' lock the attribute boxes
        NumRange.ReadOnly = True
        txtRuleTypeName.ReadOnly = True
        cboCoreRule.AllowDrop = False
        cboAppliesDuring.AllowDrop = False
        txtRuleText.ReadOnly = True

        ' so people don't try to add resource requirements to a rule type when they pick it.
        lstResources.Enabled = False
        btnAdd.Enabled = False
        btnRemove.Enabled = False

        ' populate the core rule combo box... for now, its just put in at design time.
        ' TODO: Properly run through the CoreRule enumerations to populate the combobox.
        PopulateCoreRuleBox()

        If ptrCGD.RuleTypes_g.Count = 0 Then
            mCancelPressed = True
            MsgBox("There are no defined rule types. You must define rule types before you assign them.", MsgBoxStyle.Information, "No Rule Types Defined")
            Me.Hide()
            Exit Sub
        End If

        selectMode = True

        ' select the first one
        lstRuleTypes.SelectedIndex = 0

        PopulateAttributes(lstRuleTypes.SelectedItem)

    End Sub

    Private Sub PopulateCoreRuleBox()
        Dim n() As String = [Enum].GetNames(GetType(CoreRules_cgd))
        Dim i As Integer
        cboCoreRule.Items.Clear()

        For i = 0 To n.Length - 1
            cboCoreRule.Items.Add(n(i))
        Next
    End Sub

    Private Sub PopulateAttributes(ByVal rult As cmplib01.ResourceConsumingRuleType)
        Try
            txtRuleTypeName.Text = rult.RuleName
            txtRuleText.Text = rult.RuleText
            NumRange.Value = rult.BasicRange
            cboCoreRule.Text = rult.CoreRule.ToString
            cboAppliesDuring.Text = rult.AppliesDuring.ToString

            ListResources()

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub ListResources()

        If Not TypeOf ptrNewRuleType Is ResourceConsumingRuleType Then Exit Sub

        Dim consumable As ResourceCost
        Dim consumer As ResourceConsumingRuleType = ptrNewRuleType

        For Each consumable In consumer.Costs
            lstResources.Items.Add(consumable)
        Next

    End Sub

    Private Sub PopulateSelectMode()
        ' used on exit to assign properties to the provided rule (allowing selection)
        If lstRuleTypes.SelectedItem Is Nothing Then
            MsgBox("You must select a resource consuming rule type from the list on the left.", MsgBoxStyle.Exclamation, "Select a resource consuming rule type")
            Exit Sub
        End If

        Dim selRuleType As cmplib01.ResourceConsumingRuleType = lstRuleTypes.SelectedItem

        ptrRule.usesRemaining = selRuleType.MaxUses
        'ptrRule.ptr = selRuleType.RuleName
        'ptrRule = selRuleType

    End Sub

    Private Sub PopulateAddMode()
        ' used on exit to add a new rule type

        ' if there are resources in the box, we have to convert this type into a consumable.
        If lstResources.Items.Count > 0 Then
            Dim resCost As ResourceCost
            ptrNewRuleType.Costs.Clear()
            For Each resCost In lstResources.Items
                ptrNewRuleType.Costs.Add(resCost)
            Next
        Else
            ptrNewRuleType.Costs.Clear() '// just kill them ALLLLLLL!!
        End If

        Try
            ptrNewRuleType.AppliesDuring = cboAppliesDuring.SelectedIndex
        Catch ex As Exception
            ptrNewRuleType.AppliesDuring = cmplib01.RuleApplies_cgd.Constant
            Debug.WriteLine("Err!! " & ex.Message & " in PopulateAddMode")
        End Try

        Try
            ptrNewRuleType.CoreRule = cboCoreRule.SelectedIndex
        Catch ex As Exception
            ptrNewRuleType.CoreRule = cmplib01.CoreRules_cgd.None
            Debug.WriteLine("Err!! " & ex.Message & " in PopulateAddMode")
        End Try

        ptrNewRuleType.BasicRange = NumRange.Value
        ptrNewRuleType.MaxUses = 1
        ptrNewRuleType.RuleName = txtRuleTypeName.Text
        ptrNewRuleType.RuleText = txtRuleText.Text

    End Sub

    Public ReadOnly Property CancelPressed() As Boolean
        Get
            Return mCancelPressed
        End Get
    End Property

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lstRuleTypes As System.Windows.Forms.ListBox
    Friend WithEvents pnlProperties As System.Windows.Forms.Panel
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents lblBasicRange As System.Windows.Forms.Label
    Friend WithEvents cboCoreRule As System.Windows.Forms.ComboBox
    Friend WithEvents lblCoreRule As System.Windows.Forms.Label
    Friend WithEvents lblApplies As System.Windows.Forms.Label
    Friend WithEvents txtRuleText As System.Windows.Forms.TextBox
    Friend WithEvents lblRuleText As System.Windows.Forms.Label
    Friend WithEvents cboAppliesDuring As System.Windows.Forms.ComboBox
    Friend WithEvents lblRuleTypeName As System.Windows.Forms.Label
    Friend WithEvents txtRuleTypeName As System.Windows.Forms.TextBox
    Friend WithEvents NumRange As System.Windows.Forms.NumericUpDown
    Friend WithEvents lblMiles As System.Windows.Forms.Label
    Friend WithEvents lstResources As System.Windows.Forms.ListBox
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents btnRemove As System.Windows.Forms.Button
    Friend WithEvents lblResourceConsumption As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmRulePicker))
        Me.lstRuleTypes = New System.Windows.Forms.ListBox
        Me.pnlProperties = New System.Windows.Forms.Panel
        Me.lblResourceConsumption = New System.Windows.Forms.Label
        Me.btnRemove = New System.Windows.Forms.Button
        Me.btnAdd = New System.Windows.Forms.Button
        Me.lstResources = New System.Windows.Forms.ListBox
        Me.lblMiles = New System.Windows.Forms.Label
        Me.NumRange = New System.Windows.Forms.NumericUpDown
        Me.txtRuleTypeName = New System.Windows.Forms.TextBox
        Me.lblRuleTypeName = New System.Windows.Forms.Label
        Me.txtRuleText = New System.Windows.Forms.TextBox
        Me.lblRuleText = New System.Windows.Forms.Label
        Me.lblApplies = New System.Windows.Forms.Label
        Me.cboAppliesDuring = New System.Windows.Forms.ComboBox
        Me.lblCoreRule = New System.Windows.Forms.Label
        Me.cboCoreRule = New System.Windows.Forms.ComboBox
        Me.lblBasicRange = New System.Windows.Forms.Label
        Me.btnOK = New System.Windows.Forms.Button
        Me.btnCancel = New System.Windows.Forms.Button
        Me.pnlProperties.SuspendLayout()
        CType(Me.NumRange, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lstRuleTypes
        '
        Me.lstRuleTypes.Location = New System.Drawing.Point(8, 8)
        Me.lstRuleTypes.Name = "lstRuleTypes"
        Me.lstRuleTypes.Size = New System.Drawing.Size(144, 368)
        Me.lstRuleTypes.TabIndex = 0
        '
        'pnlProperties
        '
        Me.pnlProperties.BackColor = System.Drawing.SystemColors.ControlLight
        Me.pnlProperties.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlProperties.Controls.Add(Me.lblResourceConsumption)
        Me.pnlProperties.Controls.Add(Me.btnRemove)
        Me.pnlProperties.Controls.Add(Me.btnAdd)
        Me.pnlProperties.Controls.Add(Me.lstResources)
        Me.pnlProperties.Controls.Add(Me.lblMiles)
        Me.pnlProperties.Controls.Add(Me.NumRange)
        Me.pnlProperties.Controls.Add(Me.txtRuleTypeName)
        Me.pnlProperties.Controls.Add(Me.lblRuleTypeName)
        Me.pnlProperties.Controls.Add(Me.txtRuleText)
        Me.pnlProperties.Controls.Add(Me.lblRuleText)
        Me.pnlProperties.Controls.Add(Me.lblApplies)
        Me.pnlProperties.Controls.Add(Me.cboAppliesDuring)
        Me.pnlProperties.Controls.Add(Me.lblCoreRule)
        Me.pnlProperties.Controls.Add(Me.cboCoreRule)
        Me.pnlProperties.Controls.Add(Me.lblBasicRange)
        Me.pnlProperties.Location = New System.Drawing.Point(160, 8)
        Me.pnlProperties.Name = "pnlProperties"
        Me.pnlProperties.Size = New System.Drawing.Size(264, 368)
        Me.pnlProperties.TabIndex = 1
        '
        'lblResourceConsumption
        '
        Me.lblResourceConsumption.Location = New System.Drawing.Point(8, 136)
        Me.lblResourceConsumption.Name = "lblResourceConsumption"
        Me.lblResourceConsumption.Size = New System.Drawing.Size(192, 16)
        Me.lblResourceConsumption.TabIndex = 8
        Me.lblResourceConsumption.Text = "Resources Used"
        '
        'btnRemove
        '
        Me.btnRemove.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnRemove.Location = New System.Drawing.Point(192, 248)
        Me.btnRemove.Name = "btnRemove"
        Me.btnRemove.Size = New System.Drawing.Size(56, 23)
        Me.btnRemove.TabIndex = 6
        Me.btnRemove.Text = "Remove"
        '
        'btnAdd
        '
        Me.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnAdd.Location = New System.Drawing.Point(128, 248)
        Me.btnAdd.Name = "btnAdd"
        Me.btnAdd.Size = New System.Drawing.Size(56, 23)
        Me.btnAdd.TabIndex = 5
        Me.btnAdd.Text = "Add"
        '
        'lstResources
        '
        Me.lstResources.Location = New System.Drawing.Point(8, 160)
        Me.lstResources.Name = "lstResources"
        Me.lstResources.Size = New System.Drawing.Size(248, 82)
        Me.lstResources.TabIndex = 4
        '
        'lblMiles
        '
        Me.lblMiles.Location = New System.Drawing.Point(160, 40)
        Me.lblMiles.Name = "lblMiles"
        Me.lblMiles.Size = New System.Drawing.Size(40, 16)
        Me.lblMiles.TabIndex = 13
        Me.lblMiles.Text = "miles"
        '
        'NumRange
        '
        Me.NumRange.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.NumRange.DecimalPlaces = 1
        Me.NumRange.Location = New System.Drawing.Point(80, 38)
        Me.NumRange.Maximum = New Decimal(New Integer() {250, 0, 0, 0})
        Me.NumRange.Name = "NumRange"
        Me.NumRange.Size = New System.Drawing.Size(72, 20)
        Me.NumRange.TabIndex = 1
        Me.NumRange.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        Me.NumRange.Value = New Decimal(New Integer() {1, 0, 0, 65536})
        '
        'txtRuleTypeName
        '
        Me.txtRuleTypeName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtRuleTypeName.Location = New System.Drawing.Point(79, 8)
        Me.txtRuleTypeName.Name = "txtRuleTypeName"
        Me.txtRuleTypeName.Size = New System.Drawing.Size(177, 20)
        Me.txtRuleTypeName.TabIndex = 0
        Me.txtRuleTypeName.Text = "New Rule"
        '
        'lblRuleTypeName
        '
        Me.lblRuleTypeName.Location = New System.Drawing.Point(7, 11)
        Me.lblRuleTypeName.Name = "lblRuleTypeName"
        Me.lblRuleTypeName.Size = New System.Drawing.Size(72, 16)
        Me.lblRuleTypeName.TabIndex = 10
        Me.lblRuleTypeName.Text = "Type Name"
        Me.lblRuleTypeName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'txtRuleText
        '
        Me.txtRuleText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtRuleText.Location = New System.Drawing.Point(8, 296)
        Me.txtRuleText.Multiline = True
        Me.txtRuleText.Name = "txtRuleText"
        Me.txtRuleText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtRuleText.Size = New System.Drawing.Size(248, 64)
        Me.txtRuleText.TabIndex = 7
        Me.txtRuleText.Text = "Enter a description."
        '
        'lblRuleText
        '
        Me.lblRuleText.Location = New System.Drawing.Point(8, 280)
        Me.lblRuleText.Name = "lblRuleText"
        Me.lblRuleText.Size = New System.Drawing.Size(72, 16)
        Me.lblRuleText.TabIndex = 8
        Me.lblRuleText.Text = "Rule Text"
        '
        'lblApplies
        '
        Me.lblApplies.Location = New System.Drawing.Point(7, 104)
        Me.lblApplies.Name = "lblApplies"
        Me.lblApplies.Size = New System.Drawing.Size(72, 16)
        Me.lblApplies.TabIndex = 7
        Me.lblApplies.Text = "Applies"
        Me.lblApplies.Visible = False
        '
        'cboAppliesDuring
        '
        Me.cboAppliesDuring.Items.AddRange(New Object() {"Constant", "Combat", "Non Combat"})
        Me.cboAppliesDuring.Location = New System.Drawing.Point(79, 104)
        Me.cboAppliesDuring.Name = "cboAppliesDuring"
        Me.cboAppliesDuring.Size = New System.Drawing.Size(177, 21)
        Me.cboAppliesDuring.TabIndex = 3
        Me.cboAppliesDuring.Text = "Constant"
        Me.cboAppliesDuring.Visible = False
        '
        'lblCoreRule
        '
        Me.lblCoreRule.Location = New System.Drawing.Point(8, 72)
        Me.lblCoreRule.Name = "lblCoreRule"
        Me.lblCoreRule.Size = New System.Drawing.Size(72, 16)
        Me.lblCoreRule.TabIndex = 5
        Me.lblCoreRule.Text = "Core Rule"
        Me.lblCoreRule.Visible = False
        '
        'cboCoreRule
        '
        Me.cboCoreRule.Items.AddRange(New Object() {"None", "Bridging", "Engineering", "Protected", "Air Drop", "Transport", "Transportable", "Special Operations", "Deployment", "Fieldcraft"})
        Me.cboCoreRule.Location = New System.Drawing.Point(80, 72)
        Me.cboCoreRule.Name = "cboCoreRule"
        Me.cboCoreRule.Size = New System.Drawing.Size(176, 21)
        Me.cboCoreRule.TabIndex = 2
        Me.cboCoreRule.Text = "None"
        Me.cboCoreRule.Visible = False
        '
        'lblBasicRange
        '
        Me.lblBasicRange.Location = New System.Drawing.Point(8, 40)
        Me.lblBasicRange.Name = "lblBasicRange"
        Me.lblBasicRange.Size = New System.Drawing.Size(72, 16)
        Me.lblBasicRange.TabIndex = 0
        Me.lblBasicRange.Text = "Basic Range"
        '
        'btnOK
        '
        Me.btnOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.Location = New System.Drawing.Point(304, 384)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(56, 23)
        Me.btnOK.TabIndex = 2
        Me.btnOK.Text = "OK"
        '
        'btnCancel
        '
        Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCancel.Location = New System.Drawing.Point(368, 384)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(56, 23)
        Me.btnCancel.TabIndex = 3
        Me.btnCancel.Text = "Cancel"
        '
        'frmRulePicker
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(434, 416)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.pnlProperties)
        Me.Controls.Add(Me.lstRuleTypes)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmRulePicker"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Pick Rule"
        Me.pnlProperties.ResumeLayout(False)
        Me.pnlProperties.PerformLayout()
        CType(Me.NumRange, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If selectMode = True Then
            PopulateSelectMode()
        Else
            PopulateAddMode()
        End If

        mCancelPressed = False
        Me.Hide()

    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        mCancelPressed = True
        Me.Hide()
    End Sub

    Private Sub lstRuleTypes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstRuleTypes.SelectedIndexChanged
        Try
            PopulateAttributes(lstRuleTypes.SelectedItem)
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        AddResource()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        RemoveResource()
    End Sub

    Private Sub AddResource()

        Try
            Dim selectedResource As New Resource
            Dim fRes As New frmResourceCostPicker
            Dim resCost As New ResourceCost

            fRes.CGD = ptrCGD

            fRes.ShowDialog()

            If Not fRes.CancelPressed Then
                resCost.ResourceCost = fRes.Cost
                resCost.ResourceTypeID = fRes.SelectedResourceType.ResourceTypeID
                resCost.ResourceTypeName = fRes.SelectedResourceType.ResourceName
                lstResources.Items.Add(resCost)
            End If

        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub RemoveResource()
        If Not lstResources.SelectedItem Is Nothing Then
            lstResources.Items.Remove(lstResources.SelectedItem)
        End If
    End Sub

End Class